package com.nbjtjc.safe.api.emg.group;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgGroupMapper;
import com.nbjtjc.safe.model.emg.EmgGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/group")
@CrossOrigin("*")
public class GroupController extends AbstractController<EmgGroup> {

    @Autowired
    private EmgGroupMapper emgGroupMapper;

    @Transactional(rollbackFor = {Throwable.class})
    @RequestMapping(value = "/addOrUpdate", method = {RequestMethod.POST})
    @SysLog("添加应急队伍")
    public Result add(EmgGroup emgGroup) {
        Result result = new Result();
        if (emgGroup == null) {
            result.setFalse("不能为空");
            return result;
        }
        try {
            if (emgGroup.getId() != null) {
                emgGroupMapper.updateByPrimaryKeySelective(emgGroup);
            } else {
                emgGroupMapper.insertSelective(emgGroup);
            }
            result.setData(emgGroup);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }


    @Override
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @Transactional(rollbackFor = {Throwable.class})
    @SysLog("删除应急队伍")
    public Result delete(int id) {
        Result result = new Result();
        try {
            emgGroupMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @Override
    @RequestMapping(value = "/find", method = {RequestMethod.POST})
    public Result find(EmgGroup emgGroup, Page page) {
        Result result = new Result();
        try {
            PageHelper.startPage(page.getPageCurrent(), page.getPageSize());
            List<EmgGroup> emgGroups = emgGroupMapper.search(emgGroup);
            result.setData(new PageInfo<>(emgGroups));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
}
